package com.example.test_app;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;

import com.example.sortndk.OhuangUtil;
import com.example.test_app.Util.SpUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

import skin.support.SkinCompatManager;
import skin.support.app.SkinAppCompatViewInflater;
import skin.support.app.SkinCardViewInflater;
import skin.support.constraint.app.SkinConstraintViewInflater;
import skin.support.design.app.SkinMaterialViewInflater;

public class APP extends Application {
    private static final String KEY_EXTRA_TARGET_INTENT = "EXTRA_TARGET_INTENT";
    private static final String TAG = "Application";

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);


    }

    //每次进入app，遍历assets目录下所有的文件，是否在data/data目录下都已经存在，不存在则拷贝

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void initAssetsFile(String assetsPath, String path) {

        boolean needCopy = false;

        Log.d(TAG, "initAssetsFile: path=" + path);
        // 遍历assets目录下所有的文件，是否在data/data目录下都已经存在
        try {
            String[] fileNames = getApplicationContext().getAssets().list(assetsPath);
            for (int i = 0; fileNames != null && i < fileNames.length; i++) {
                Log.d(TAG, "initAssetsFile: assetsPath=" + assetsPath + " files=" + Arrays.toString(fileNames));
                if (!FileUtil.checkFileExist(path, fileNames[i])) {
                    needCopy = true;
                    break;
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        if (needCopy) {
            FileUtil.copyFilesAssets(getApplicationContext(), assetsPath, path);
        }

    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onCreate() {
        super.onCreate();
        if (isMainProcess()) {
            SpUtil.init(this);
            SkinCompatManager.withoutActivity(this)
                    .addInflater(new SkinAppCompatViewInflater()) // 基础控件换肤初始化
                    .addInflater(new SkinMaterialViewInflater()) // material design 控件换肤初始化[可选]
                    .addInflater(new SkinConstraintViewInflater()) // ConstraintLayout 控件换肤初始化[可选]
                    .addInflater(new SkinCardViewInflater()) // CardView v7 控件换肤初始化[可选]
                    .setSkinStatusBarColorEnable(true) // 关闭状态栏换肤，默认打开[可选]
                    .setSkinWindowBackgroundEnable(false) // 关闭windowBackground换肤，默认打开[可选]
                    .loadSkin();
            getCPUABI();
            String abi = CPUABI;
            File file = getApplicationContext().getDataDir();

            String path = file.toString() + "/" + abi + "/";
            initAssetsFile(abi, path);

            String path2 = file.toString() + "/key/";
            initAssetsFile("key", path2);

            OhuangUtil.get().init(path);


//            System.setProperty("javax.net.ssl.trustStore", path2+"hj.jks");
        }
    }


    private boolean isMainProcess() {
        int pid = android.os.Process.myPid();
        Log.d(TAG, "isMainProcess: mypid=" + pid + " pageName=" + getApplicationInfo().packageName);
        android.app.ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
            Log.d(TAG, "isMainProcess: appProcess.pid=" + appProcess.pid + " pagename=" + appProcess.processName);
            if (appProcess.pid == pid) {
                return getApplicationInfo().packageName == appProcess.processName;
            }
        }
        return false;
    }

    public static String CPUABI = null;

    public static void getCPUABI() {
        if (CPUABI == null) {
            try {
                String os_cpuabi = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec("getprop ro.product.cpu.abi").getInputStream())).readLine();
                if (os_cpuabi.contains("x86")) {
                    CPUABI = "x86";
                } else if (os_cpuabi.contains("armeabi-v7a") || os_cpuabi.contains("arm64-v8a")) {
                    CPUABI = "armeabi-v7a";
                } else {
                    CPUABI = "armeabi";
                }
            } catch (Exception e) {
                CPUABI = "armeabi";
            }
        }
    }

}
